package com.mineapp.main

import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.KeyEvent
import android.widget.Toast
import com.http.HttpCallback
import com.join51.gouganjue.model.StoreData
import com.mineapp.R
import com.mineapp.base.BaseActivity
import com.mineapp.service.APIService
import com.mineapp.weight.LoadingDialog
import com.tool.KToast

class MainActivity : BaseActivity() {
    private var mDownCount = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val dialog = LoadingDialog(this)
        dialog.show()
        Thread.sleep(2000)
        val service = mRetrofit?.create(APIService::class.java)
        val call = service?.getStore("1", "10", "MERGQzcyMjM1RDYwN0RFOEI1ODQwMUYxODZBREVERDU")
        call?.enqueue(HttpCallback<StoreData>().start({
            url: String?, data: StoreData? ->
            KToast.shortToast(this, url)
        }, { url: String?, code: Int?, msg: String? ->
            KToast.shortToast(this, msg)
        }))
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mDownCount == 0) {
                Toast.makeText(this, "再按一次，退出应用", Toast.LENGTH_SHORT).show()
            }
            mDownCount++
            Handler().postDelayed({
                if (mDownCount > 1) {
                    System.exit(0)
                } else {
                    mDownCount = 0
                }
            }, 500)
            return true
        }
        return false
    }

}

